package Classes
{
	import Classes.Components.AIComponent;
	import Classes.Components.ActionComponent;
	import Classes.Components.PlayerComponent;
	import Classes.Components.RigidBodyComponent;
	import Classes.Components.SkillComponent;
	
	import flash.display.Sprite;
	import flash.geom.Point;

	public class EntityManager
	{
		private var m_world:World;
		private var m_entities:Vector.<Entity> = new Vector.<Entity>;
		private var m_entityPrototypes:Object = new Object();
		
		public function EntityManager()
		{
			for each(var entityConfig:Object in Main.configManager.getEntities()){
				var entity:Entity = new Entity(entityConfig.attributes['name']);
				m_entityPrototypes[entity.config.attributes['name']] = entity;
			}
		}
		
		public function addEntity(entity:Entity):void{
			m_entities.push(entity);
		}
		
		public function removeEntity(entity:Entity):void{
			m_entities.splice(m_entities.indexOf(entity),1);
		}
		
		private function getSkillfulEntity(name:String, position:Point):Entity{
			var entity:Entity = getBaseEntity(name, position);
			entity.addComponent(new SkillComponent(entity));
			return entity;
		}
		
		private function getBaseEntity(name:String, position:Point):Entity{
			var entity:Entity = new Entity(name);
			var ac:ActionComponent = new ActionComponent(entity);
			var rbc:RigidBodyComponent = new RigidBodyComponent(entity);
			
			entity.addComponent(ac);
			entity.addComponent(rbc);
			
			var bg:Sprite = new Sprite();						
			bg.graphics.beginFill(entity.getColor());
			bg.graphics.drawCircle(0,0,rbc.getSize()*Main.P2WSCALE);
			bg.graphics.endFill();
			
			rbc.getDisplay().addChild(bg);
			rbc.setPosition(position);
			rbc.attach(m_world);
			rbc.forceUpdate();
			
			return entity;			
		}
		
		public function pickEntity(point:Point):Entity{
			var i:int;
			var ent:Entity;
			var length:int = m_entities.length;
			var entities:Vector.<Entity> = m_entities;
			for(i=0;i<length;i++){
				ent = entities[i];
				if(ent.getRigidBodyComponent().detectCollisionWithPoint(point)){
					return ent;
				}
			}
			
			return null;
		}
		
		public function setWorld(world:World):void{
			m_world = world;
		}
		
		public function createPlayer():Entity{
			var entity:Entity = getSkillfulEntity('player', new Point(0,0));	
			entity.addComponent(new PlayerComponent(entity));
			entity.getSkillComponent().addSkillByName('fireball');
			entity.getSkillComponent().addSkillByName('standard_attack');
			
			return entity;
		}
		
		public function createMob(name:String, position:Point):Entity{
			var entity:Entity = getSkillfulEntity(name, position);
			entity.addComponent(new AIComponent(entity));

			entity.getSkillComponent().addSkillByName('standard_attack');
			
			entity.getActionComponent().setTarget(m_world.getPlayer());
			return entity;
		}
		
		
		public function createSkill(skill:Skill):Entity{
			var startPos:Point = skill.owner.getRigidBodyComponent().getPosition();
			var entity:Entity = getBaseEntity(skill.name, startPos);
			entity.addComponent(new AIComponent(entity));
			
			return entity;			
		}		
		
		public function getMobs():Array{
			var mobs:Array = [];
			for each(var entity:Entity in m_entityPrototypes){
				if(entity.hasTag('mob')){
					mobs.push(entity);
				}
			}
			
			return mobs;
		}		
		
	}
}